package com.mo.lib.ui.dialog

import android.app.Activity
import android.content.Intent
import android.graphics.BitmapFactory
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.dylanc.longan.topActivity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.mo.lib.R
import com.mo.lib.utils.*
import com.mo.lib.utils.data.isEmpty
import com.wildma.pictureselector.PictureSelectUtils
import com.xuexiang.xutil.file.FileUtils

/**
 * @ author：mo
 * @ data：2018/11/22
 * @ 功能：拍照或者从相册取图片
 */
open class PhoneTakeDialog(val isAuto: Boolean = true) : BaseDialog(R.layout.dialog_phone_take) {
    override fun doWhat() { //拍照
        dialog.findViewById<View>(R.id.btn_take_photo).setClick {
            if (isExistCamera()) {
                XXPermissions.with(topActivity).permission(Permission.CAMERA,Permission.READ_MEDIA_IMAGES, Permission.READ_MEDIA_VIDEO,Permission.READ_MEDIA_AUDIO
                ).request(object :
                    OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                        if (all) {
                            if (isAuto) {
                                PictureSelectUtils.getByCamera(topActivity)
                            }
                            actionPhoneTake()
                        } else {
                            showToast("缺少相关权限，请重试!")
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                        super.onDenied(permissions, never)
                        if (never) {
                            showToast("被永久拒绝授权，请手动授予拍照和存储权限") // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(topActivity, permissions)
                        } else {
                            showToast("请手动授予拍照和存储权限")
                        }
                    }
                })
            } else {
                showToast("没有找到拍照设备！")
            }
            dismisss()
        } //从相册选
        dialog.findViewById<View>(R.id.btn_pick_photo).setClick {
            if (FileUtils.isSDCardExist()) {
                XXPermissions.with(topActivity).permission(Permission.READ_MEDIA_IMAGES, Permission.READ_MEDIA_VIDEO,Permission.READ_MEDIA_AUDIO).request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                        if (all) {
                            if (isAuto) {
                                PictureSelectUtils.getByAlbum(topActivity)
                            }
                            actionPhoneChoose()
                        } else {
                            showToast("缺少相关权限，请重试!")
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                        super.onDenied(permissions, never)
                        if (never) {
                            showToast("被永久拒绝授权，请手动授予存储权限") // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(topActivity, permissions)
                        } else {
                            showToast("获取存储权限失败")
                        }
                    }
                })
            } else {
                showToast("SD卡不可用")
            }
            dismisss()
        } //取消
        dialog.findViewById<View>(R.id.btn_cancel).setClick { dismisss() }
    }

    open fun actionPhoneChoose() {}
    open fun actionPhoneTake() {}

}
/** 在调用弹窗的activity里的onActivityResult里按照下面这个方法写，拍照返回的图片在有些机型上会旋转角度 */
fun  doWhatMedia( activity: Activity,requestCode: Int, resultCode: Int, data: Intent?,block:(String)->Unit){
    if (resultCode == AppCompatActivity.RESULT_CANCELED){
        showToast("取消操作")
    }else{
        when (requestCode) {
            PictureSelectUtils.GET_BY_CAMERA -> {
                val picturePath = PictureSelectUtils.onActivityResult(activity, requestCode, resultCode, data, false, 0, 0, 0, 0)
                if (!isEmpty(picturePath)) {
                    val degree = getRotateDegree(picturePath)
                    if (degree != 0) {
                        saveFile(picturePath, getBitmapForRotate(BitmapFactory.decodeFile(picturePath), degree))
                    }
                    block.invoke(picturePath)
                }
            }
            PictureSelectUtils.GET_BY_ALBUM -> {
                val picturePath2 = PictureSelectUtils.onActivityResult(activity, requestCode, resultCode, data, false, 200, 200, 1, 1)
                block.invoke(picturePath2)
            }
            else -> {
            }
        }

    }
}
/** 例*/
//override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//    super.onActivityResult(requestCode, resultCode, data)
//    doWhatMedia(activity,requestCode,resultCode,data){
//        viewModel?.upLogoFile(BitmapFactory.decodeFile(it))
//    }
//}